package com.hongxin.nw.netty.interceptor;

import com.hongxin.nw.netty.utils.pools.ClientPools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @remark --
 * @author: GuoXiaolin
 * @date 2019/5/22 19:03
 * @Version 1.0
 * @Descibe
 */
@Slf4j
public class AuthorityInterceptor extends HandlerInterceptorAdapter {
	
	 @Override
	 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
         String ip = request.getRemoteHost();
         String method = request.getRequestURI();
         String str = ip + "-" + method;
         long currRequest = System.currentTimeMillis()/1000;
         long preRequest = ClientPools.getClient(str);
         if (preRequest == 0L || currRequest - preRequest > 2){
//             log.info("正常请求:{}",str);
             ClientPools.addClient(str, currRequest);
             return true;
         }else{//(currRequest - preRequest <= 2)
//             log.info("异常请求:{}",str);
             ClientPools.addClient(str, currRequest);
             return false;
         }
    }
}